package com.shsmi.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shsmi.application.CoordsConvert;
import com.shsmi.entity.CityPoint;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.shsmi.application.CoordsConvert.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import static com.shsmi.util.CreateFileUtil.createJsonFile;




@Component
@EnableScheduling
public class Convert {
    public static String tokenStr;

    public static void main(String[] args) {
        convertShCoord();
        System.out.println("token----" + tokenStr);
    }

    @Scheduled(cron = "0 0 1 * * ?")
    public static void convertShCoord() {
        //读写json
        String jsonStr = new ClassPathResourceReader("static/data.json").getContent();

        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        JSONArray jsonArrayList = jsonObject.getJSONArray("result");
        JSONArray result=new JSONArray();
        for (Object obj : jsonArrayList) {
            JSONObject retJson = new JSONObject();
            JSONObject jsonObject1 = JSONObject.parseObject(obj.toString());
            double lon =Double.valueOf(jsonObject1.get("lng").toString());
            double lat =Double.valueOf(jsonObject1.get("lat").toString());
            CoordsConvert coordsConvert =new CoordsConvert();
            CityPoint newCityPoint=  coordsConvert.cgcs2city(lon,lat);
            System.out.println("newCityPoint----" + newCityPoint);
            result.add(newCityPoint);
        }
        String resultStr=result.toJSONString();
        createJsonFile(resultStr, "D:/", "converCoord");
        System.out.println("jsonArrayList----" + jsonArrayList);
    }

}
